<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\facade\Session;

function login_member(){
  return session("member");
}

function login_member_id(){
    $member = login_member();
    if(empty($member)){
        $id = 0;
    }else{
        $id = intval($member['id']);
    }
    return $id;
}

function login_user(){
  return session("user");
}

function login_user_id(){
  $user = login_user();
  if(empty($user)){
    $id = 0;
  }else{
    $id = intval($user['id']);
  }
  return $id;
}

function left_menu_item_class($current_ancestors, $menu_item){
  $class_name = "";
  if(isset($current_ancestors[$menu_item['id']])){
    if(empty($menu_item["children"])){
      $class_name = "active";
    }else{
      $class_name = "active open";
    }
  }
  return $class_name;
}

/**
* 格式化显示时间
* @param int_time string 时间值
* @param type string 输入格式
* @return string
*/
function format_show_time($int_time, $type = ""){
  if(empty($int_time)){
    return "";
  }else if(is_numeric($int_time)){
    switch ($type) {
    case 'zh_time':
      $format_str = "m月d日 H:i:s";
      break;
    case 'no_second':
      $format_str = "Y-m-d H:i";
      break;
    case 'date_node':
      $format_str = "Y.m.d";
      break;
    case 'small_date_node':
      $format_str = "y.m.d";
      break;
    case 'no_year_node':
      $format_str = "m.d H:i:s";
      break;
    case 'short_node':
      $format_str = "m.d";
      break;
    case 'only_date':
      $format_str = "Y-m-d";
      break;
    case 'month_day':
      $format_str = "m-d";
      break;
    case 'date_dir':
      $ds = DIRECTORY_SEPARATOR;
      $format_str = "Y{$ds}m{$ds}d";
      break;
    case 'ymd_dir':
      $format_str = "Ymd";
      break;
    case 'weekday':
      $format_str = "D";
      break;
    default:
      $format_str = "Y-m-d H:i:s";
      break;
    }
    return date($format_str, $int_time);
  }else{
    return $int_time;
  }
}

// 应用公共文件
function get_field_text($field_name, $val){
  if(is_null($val) || $val === ''){
    return '';
  }
  switch ($field_name) {
  case "status":
    $list = list_field_status();
    break;
  case "success":
    $list = list_field_success();
    break;
  case "menu_status":
    $list = list_field_menu_status();
    break;
  case "user_status":
    $list = list_field_user_status();
    break;
  case "article_type":
    $list = list_field_article_type();
    break;
  case "open_targets":
    $list = list_field_open_target();
    break;
  case "education":
    $list = list_field_education();
    break;
  case "college_type":
    $list = list_field_college_type();
    break;
  case "pro_title":
    $list = list_field_pro_title();
    break;
  case "user_gender":
    $list = list_field_user_gender();
    break;
  case "sms_type":
    $list = list_field_sms_type();
    break;
  default:
    $list = [];
    break;
  }

  if(isset($list[$val])){
    return $list[$val];
  }else{
    return $val;
  }
}

function list_field_status(){
  return ["0" => "草稿", "1" => "发布"];
}

function list_field_menu_status(){
  return ["1" => "显示", "0" => "隐藏"];
}

function list_field_user_status(){
  return ["1" => "启用", "0" => "冻结"];
}

function list_field_success(){
  return ["0" => "失败", "1" => "成功"];
}

function list_field_user_gender(){
  return ["secret" => "保密", "male" => "男", "female" => "女", "unknown" => "未知"];
}

function list_field_education(){
  return [
    // "secret" => "保密",
    // "zhongji" => "中技",
    // "zhongzhuan" => "中专",
    // "gaozhong" => "高中",
    "dazhuan" => "专科",
    "benke" => "本科",
    "shuoshi" => "硕士研究生",
    "boshi" => "博士研究生",
    // "boshihou" => "博士后",
    // "mba" => "MBA",
  ];
}

function list_field_college_type(){
  return [
    "benke" => "本科",
    "gaozhi" => "高职",
    "zhongzhi" => "中职",
  ];
}

function list_field_pro_title(){
  return [
    "jiaoshou" => "教授",
    "fujiaoshou" => "副教授",
    "jiangshi" => "讲师",
    "zhujiao" => "助教",
  ];
}

function list_field_article_type(){
  return ["" => "未知", "News" => "新闻", "Note" => "笔记"];
}

function list_field_open_target(){
  return ["_self" => "当前窗口", "_blank" => "新窗口"];
}

function list_field_sms_type(){
  return ['register' => '用户注册', 'reset_info' => '修改资料', 'change_mobile' => '更换手机'];
}

/**
 * GET 请求
 * @param   string  $url 请求地址
 */
function http_get($url){
    //初始化
    $ch = curl_init();
    //设置抓取的url
    curl_setopt($ch, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //执行命令
    $data = curl_exec($ch);
    //关闭URL请求
    curl_close($ch);
    //显示获得的数据
    return $data;
 }


/**
* POST 请求
* @Author   zhanghong
* @DateTime 2016-03-10
* @param    string     $url    请求地址
* @param    string     $param  请求参数
* @param    array      $header 请求头部信息
* @param    boolean    $post_file 上传文件
* @return   array/false       接口返回数据
*/
function http_post($url, $param, $header = array(), $post_file=false){
    $oCurl = curl_init();
    if(stripos($url,"https://")!==FALSE){
      curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    if($header){
      curl_setopt ( $oCurl, CURLOPT_HTTPHEADER, $header );
    }
    if (is_string($param) || $post_file) {
      $strPOST = $param;
    } else {
      $aPOST = array();
      foreach($param as $key=>$val){
        $aPOST[] = $key."=".urlencode($val);
      }
      $strPOST =  join("&", $aPOST);
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($oCurl, CURLOPT_POST,true);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
    // var_dump($oCurl);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    return $sContent;
}